{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import svm\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dataset = pd.read_csv('data/table_4_a_watermelon_3_alpha.csv')\n",
    "dataset = dataset.drop('Idx',axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>density</th>\n",
       "      <th>sugar</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.697</td>\n",
       "      <td>0.460</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.774</td>\n",
       "      <td>0.376</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.634</td>\n",
       "      <td>0.264</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.608</td>\n",
       "      <td>0.318</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.556</td>\n",
       "      <td>0.215</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.403</td>\n",
       "      <td>0.237</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.481</td>\n",
       "      <td>0.149</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.437</td>\n",
       "      <td>0.211</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.666</td>\n",
       "      <td>0.091</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.243</td>\n",
       "      <td>0.267</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.245</td>\n",
       "      <td>0.057</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.343</td>\n",
       "      <td>0.099</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.639</td>\n",
       "      <td>0.161</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.657</td>\n",
       "      <td>0.198</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.360</td>\n",
       "      <td>0.370</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.593</td>\n",
       "      <td>0.042</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.719</td>\n",
       "      <td>0.103</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     density  sugar  label\n",
       "0      0.697  0.460      1\n",
       "1      0.774  0.376      1\n",
       "2      0.634  0.264      1\n",
       "3      0.608  0.318      1\n",
       "4      0.556  0.215      1\n",
       "5      0.403  0.237      1\n",
       "6      0.481  0.149      1\n",
       "7      0.437  0.211      1\n",
       "8      0.666  0.091      0\n",
       "9      0.243  0.267      0\n",
       "10     0.245  0.057      0\n",
       "11     0.343  0.099      0\n",
       "12     0.639  0.161      0\n",
       "13     0.657  0.198      0\n",
       "14     0.360  0.370      0\n",
       "15     0.593  0.042      0\n",
       "16     0.719  0.103      0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch6.2\n",
    "试使用LIBSVM，在习惯数据集3.0α上分别用线性和和高斯核训练一个SVM，并比较其支持向量的差别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.243  0.267]\n",
      " [ 0.343  0.099]\n",
      " [ 0.639  0.161]\n",
      " [ 0.657  0.198]\n",
      " [ 0.36   0.37 ]\n",
      " [ 0.719  0.103]\n",
      " [ 0.634  0.264]\n",
      " [ 0.608  0.318]\n",
      " [ 0.556  0.215]\n",
      " [ 0.403  0.237]\n",
      " [ 0.481  0.149]\n",
      " [ 0.437  0.211]]\n"
     ]
    }
   ],
   "source": [
    "svc = svm.SVC(C=1000, kernel='linear')  # classifier 1 based on linear kernel\n",
    "svc.fit(dataset.ix[:,:-1], dataset.ix[:,-1])\n",
    "sv = svc.support_vectors_\n",
    "print(sv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.343  0.099]\n",
      " [ 0.639  0.161]\n",
      " [ 0.657  0.198]\n",
      " [ 0.36   0.37 ]\n",
      " [ 0.774  0.376]\n",
      " [ 0.556  0.215]\n",
      " [ 0.403  0.237]\n",
      " [ 0.481  0.149]\n",
      " [ 0.437  0.211]]\n"
     ]
    }
   ],
   "source": [
    "svc = svm.SVC(C=1000, kernel='rbf')  # classifier 1 based on linear kernel\n",
    "svc.fit(dataset.ix[:,:-1], dataset.ix[:,-1])\n",
    "sv = svc.support_vectors_\n",
    "print(sv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch 6.8\n",
    "以西瓜数据集3.0α的“密度”为输入，“含糖率”为输出，试使用LIBSVM训练一个SVR。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.697]\n",
      " [ 0.774]\n",
      " [ 0.608]\n",
      " [ 0.666]\n",
      " [ 0.243]\n",
      " [ 0.245]\n",
      " [ 0.343]\n",
      " [ 0.36 ]\n",
      " [ 0.593]\n",
      " [ 0.719]]\n"
     ]
    }
   ],
   "source": [
    "svc = svm.SVR(C=1000, kernel='rbf')  # classifier 1 based on linear kernel\n",
    "svc.fit(np.reshape(np.array(dataset.ix[:,0]),[-1,1]), np.array(dataset.ix[:,1]))\n",
    "sv = svc.support_vectors_\n",
    "print(sv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
